package com.educate.book.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.educate.book.model.bo.ItemBO;
import com.educate.book.model.po.Item;
import com.educate.common.mongo.QueryUtils;
import com.educate.common.web.PageableUtils;

public class ItemDaoImpl {

	@Resource
	MongoTemplate mongoTemplate;

	public Page<Item> query(ItemBO bo, Pageable pageable) {
		List<Criteria> andCriteria = new ArrayList<>(10);
		Criteria keyCriteria = QueryUtils.keyWord(bo.getName(), "name");
		if (keyCriteria != null) {
			andCriteria.add(keyCriteria);
		}
//        if (startTime != null || endTime != null) {
//            Criteria createTime = Criteria.where("createTime");
//            if (startTime != null) {
//                createTime.gt(startTime);
//            }
//            if (endTime != null) {
//                createTime.lt(endTime);
//            }
//            andCriteria.add(createTime);
//        }
		if (!StringUtils.isEmpty(bo.getItemId())) {
			andCriteria.add(Criteria.where("itemId").is(bo.getItemId()));
		}
		if(bo.getStatus()>=0) {
			andCriteria.add(new Criteria().where("status").is(bo.getStatus()));
		}
//		Query query = QueryUtils.query(andCriteria).with(pageable);
//		List<Item> list = mongoTemplate.find(query, Item.class);
//		return PageableExecutionUtils.getPage(list, pageable, () -> mongoTemplate.count(query, Item.class));
	    Query query = QueryUtils.query(andCriteria).with(pageable);
        List<Item> list = mongoTemplate.find(query, Item.class);
        return PageableUtils.getPage(list, pageable, () -> mongoTemplate.count(query, Item.class));

	}

	
	public Item queryBYParentId(String parentId) {
		Query query = new Query();
		query.addCriteria(new Criteria("itemId").is(parentId));
		return mongoTemplate.findOne(query, Item.class);
	}
	
	public Item queryMaxLevelItem(){
		Query q = new BasicQuery("{}").addCriteria(new Criteria().where("status").is(1)).with(new Sort(new Sort.Order(Sort.Direction.DESC, "level"))).limit(1);
		Item result = mongoTemplate.findOne(q, Item.class);
		return result;
	}
	
	public List<Item> queryItemByLevel(int level){
		Query query = new Query();
		query.addCriteria(new Criteria("level").is(level));
		return mongoTemplate.find(query, Item.class);
	 }
	public Item queryByParentIdAndName(String parentId,String name) {
		Query query = new Query();
		Criteria criteria = new Criteria();
		criteria.andOperator(new Criteria().where("name").is(name),new Criteria().where("parentId").is(parentId));
		query.addCriteria(criteria);
		return mongoTemplate.findOne(query, Item.class);
	}
	
	public List<Item> getItemByParentId(List<String> parentIdList){
		Query query = new Query();
		Criteria criteria = new Criteria();
		criteria.andOperator(new Criteria().where("parentId").in(parentIdList),new Criteria().where("status").is(1));
		query.addCriteria(criteria);
		return mongoTemplate.find(query, Item.class);
	}
	
	 
	
}
